package com.qingke.easyjava.jdbc.command;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import com.qingke.easyjava.jdbc.command.impl.AcceptCommand;
import com.qingke.easyjava.jdbc.command.impl.AnswerCommand;
import com.qingke.easyjava.jdbc.command.impl.AskCommand;
import com.qingke.easyjava.jdbc.command.impl.ListCommand;
import com.qingke.easyjava.jdbc.command.impl.ScoreCommand;
import com.qingke.easyjava.jdbcapp.pojo.Player;

public class PlayerCommandFactory extends SystemCommandFactory {

	private Player player;

	public PlayerCommandFactory(Player player) {
		this.player = player;
	}
	private static Map<CommandCode, Class<? extends Command>> commandMap = new HashMap<>();

	static {
		commandMap.put(CommandCode.ACCEPT, AcceptCommand.class);
		commandMap.put(CommandCode.ANSWER, AnswerCommand.class);
		commandMap.put(CommandCode.ASK, AskCommand.class);
		commandMap.put(CommandCode.LIST, ListCommand.class);
		commandMap.put(CommandCode.SCORE, ScoreCommand.class);
		
	}

	@Override
	public Command buildCommand(CommandCode cmdCode) {
		Command command = null;
		Class<? extends Command> cmdClass = commandMap.get(cmdCode);
		if(cmdClass==null){
			return super.buildCommand(cmdCode);
		}
		try {
			command=cmdClass.getConstructor(Player.class).newInstance(player);
		} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
				| NoSuchMethodException | SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

//		if (command == null) {
//			command = super.buildCommand(cmdCode);
//		}
		return command;
	}

}
